package com.youtellv1.service.Impl;

import com.github.pagehelper.PageInfo;
import com.youtellv1.entity.Post;
import com.youtellv1.mapper.PostMapper;
import com.youtellv1.service.PostService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {
    @Resource
    private PostMapper postMapper;

    @Override
    public PageInfo<Post> findPostByPage(int page, int size) {
        int offset = (page - 1) * size;
        List<Post> list = postMapper.findAllPost(offset, size);

        int total = postMapper.postCount();

        PageInfo<Post> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(size);

        return pageInfo;
    }

    @Override
    public Post findAllService(Post post) {
        return postMapper.findPostById(post);
    }

    @Override
    public void saveService(Post post) {
        postMapper.save(post);
    }

    @Override
    public void updateService(Post post) {
        postMapper.update(post);
    }

    @Override
    public void deleteService(Post post) {
        postMapper.delete(post);
    }

    @Override
    public List<Post> findAll() {
        return postMapper.findAll();
    }

    @Override
    public Integer postCountService() {
        return postMapper.postCount();
    }
}
